package backtracking.subSets;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class N90_SubSetsII {
    private List<List<Integer>> res = new LinkedList<>();
    private List<Integer> path = new LinkedList<>();

    private void backtracking(int[] nums, int startIdx) {
        res.add(new ArrayList<>(path));
        if (startIdx >= nums.length) return;
        for (int i = startIdx; i < nums.length; i++) {
            if (i > startIdx && nums[i] == nums[i - 1]) continue;
            path.add(nums[i]);
            backtracking(nums, i + 1);
            path.remove(path.size() - 1);
        }
    }

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        backtracking(nums, 0);
        return res;
    }
}
